//专门检测风扇(进料风扇,点火风扇,排气风扇)线路是否连接正常
//检测原理：
//  接上负载后, 打开负载电源
//      若负载接上, 反馈输出是矩形波(大概是50Hz或者60Hz的波形)
//      若没接负载在, 反馈输出有可能是低电平 也有可能是高电平

#include <stdint.h>
#include "main.h"
#include "temperature_control_sub_module/FanCtrl.h"


#define FanError_CooldownPeriod_MaxTime 10*60
#define ON_OFF_Detection_Pin_Sel ((1ULL<<IO_De_Hot) | (1ULL<<IO_De_Mor) | (1ULL<<IO_De_Fan))

FanError_E FanError;


//Tick 2ms
//排气风扇检测
void FanDetection_Exhaust(void)
{
    static uint16_t x = 0;
    static uint16_t count_H = 0;
    static uint16_t count_L = 0;

    //排气风扇
    if(Fan_State.Fan == 1)
    { //排气扇输出 (橙色线)
        if(gpio_get_level(IO_De_Fan) == 0)
        {
            if(count_L < 0xffff) count_L++;
        }
        if(gpio_get_level(IO_De_Fan) != 0)
        {
            if(count_H < 0xffff) count_H++;
        } 

        if(x >= 2500)
        { //Tick 5s
            x = 0;
            if((count_H >= 200) && (count_L >= 200))
            { //Normal
                UI_System.ICO_Error_Fan = UI_OFF;  
            }
            else
            {
                if(FanError == FanError_None)
                {
                    FanError = FanError_Fan;
                }
                UI_System.ICO_Error_Fan = UI_ON;  
            }
            count_H = 0;
            count_L = 0;
        }
        else x++;
    }
    else
    {
        if(x > 0) x = 0;
        if(count_H > 0) count_H = 0;
        if(count_L > 0) count_L = 0;
    }
}
//进料风扇(红色线)
void FanDetection_Auger(void)
{
    static uint16_t x = 0;
    static uint16_t count_H = 0;
    static uint16_t count_L = 0;

    //进料风扇
    if(Fan_State.Feed == 1)
    {
        if(gpio_get_level(IO_De_Mor) == 0)
        {
            if(count_L < 0xffff) count_L++;
        }
        if(gpio_get_level(IO_De_Mor) != 0)
        {
            if(count_H < 0xffff) count_H++;
        } 

        if(x >= 2500)
        {
            x = 0;
            if((count_H >= 200) && (count_L >= 200))
            { //Normal
                UI_System.ICO_Error_Auger = UI_OFF; 
            }
            else
            {
                if(FanError == FanError_None)
                {
                    FanError = FanError_Auger;
                }
                UI_System.ICO_Error_Auger = UI_ON; 
            }
            count_H = 0;
            count_L = 0;
        }
        else x++;
    }
    else
    {
        if(x > 0) x = 0;
        if(count_H > 0) count_H = 0;
        if(count_L > 0) count_L = 0;
    }
}
//点火风扇(紫色线): 目前的电路只能检测出120v点火棒, 220v点火棒接上跟不接信号一样
void FanDetection_Ignite(void)
{
    static uint16_t x = 0;
    static uint16_t count_H = 0;
    static uint16_t count_L = 0;

    if(Fan_State.Heater == 1)
    {
        if(gpio_get_level(IO_De_Hot) == 0)
        {
            if(count_L < 0xffff) count_L++;
        }
        if(gpio_get_level(IO_De_Hot) != 0)
        {
            if(count_H < 0xffff) count_H++;
        }

        if(x >= 2500)
        { //Tick 5s
            x = 0;
            if((count_H >= 200) && (count_L >= 200))
            { //Normal
                UI_System.ICO_Error_Ignite = UI_OFF;  
            }
            else
            {
                if(FanError == FanError_None)
                {
                    FanError = FanError_Ignite;
                }
                UI_System.ICO_Error_Ignite = UI_ON;  
            }
            count_H = 0;
            count_L = 0;
        }
        else x++;
    }
    else
    {
        if(x > 0) x = 0;
        if(count_H > 0) count_H = 0;
        if(count_L > 0) count_L = 0;
    }
}

//风扇开关检测, Tick 2ms
void FanDetection_Clock(void)
{
    FanDetection_Exhaust();
    FanDetection_Auger();
    FanDetection_Ignite();
}

void FanDetection_Config(void)
{
    //Pin config
    gpio_config_t io_conf;

    io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
    io_conf.mode = GPIO_MODE_INPUT;
    io_conf.pin_bit_mask = ON_OFF_Detection_Pin_Sel;
    io_conf.pull_down_en = 0;
    io_conf.pull_up_en = 1;
    gpio_config(&io_conf);
}

void FanDetection_Init(void)
{
    FanDetection_Config();
    FanError = FanError_None;
}

void FanDetection_Main(void)
{
    // if((UI_System.ICO_Error_Auger == UI_ON) || (UI_System.ICO_Error_Fan == UI_ON) || (UI_System.ICO_Error_Ignite == UI_ON)) UI_System.ICO_Error = UI_ON;
    // else UI_System.ICO_Error = UI_OFF;
    // if (FanError == FanError_Auger) RTD[RTD_Seq_RTD].status = Pt_EOr;
    // else if (FanError == FanError_Fan) RTD[RTD_Seq_RTD].status = Pt_EFn;
    // else if (FanError == FanError_Ignite) RTD[RTD_Seq_RTD].status = Pt_Elr;
}
